package com.naiterui.ehp.bs.pharmacist.controller.inner;

import java.util.Set;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.service.IRecommendService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 处方内部接口
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021/6/23 10:00
 **/
@Slf4j
@RestController
@RequestMapping("pharmacist/inner/recommend")
@RequiredArgsConstructor
@Api(tags = {SwaggerTag.PHARMACIST + SwaggerTag.INNER_API + SwaggerTag.RECOM})
public class PharmacistInnerRecommendController {

    private final IRecommendService recommendService;

    @ApiOperation("处方自动审核")
    @PostMapping("/auto/audit")
    public ResponseVO<Void> autoAudit(@RequestParam("recomId") String recomId) throws BusinessException {
        if (StringUtils.isEmpty(recomId)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        log.info("系统自动审核处方， recomId: {}", recomId);
        recommendService.autoAudit(Long.parseLong(recomId));
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ApiOperation("处方调配药师审核")
    @PostMapping("/allocate/audit")
    public ResponseVO<Void> allocateAudit(@RequestParam("recomId") Long recomId) {
        if (null == recomId) {
            log.info("处方调配药师审核recomId为空");
            PublicService.returnResponseVO(ExceptionCodes.DATA_ERR);
        }
        log.info("处方调配药师审核， recomId: {}", recomId);
        recommendService.allocateAudit(recomId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ApiOperation("处理审核药师ID")
    @PostMapping("/handle/audit/pharmacist")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pharmacistId", value = "药师id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "type", value = "类型：1.自动审核药师 2.调配审核药师", required = true, dataType = "int"),
            @ApiImplicitParam(name = "operate", value = "操作：1.新增 2.删除", required = true, dataType = "int")
    })
    public ResponseVO<Void> handleAuditPharmacist(@RequestParam("pharmacistId") Long pharmacistId, @RequestParam("type") Integer type, @RequestParam("operate") Integer operate) {
        log.info("处理审核药师Id，pharmacistId: {}, type: {}, operate: {}", pharmacistId, type, operate);
        recommendService.handleAuditPharmacist(pharmacistId, type, operate);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ApiOperation("获取处理审核药师ID")
    @GetMapping("/audit/pharmacist/{type}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型：1.自动审核药师 2.调配审核药师", required = true, dataType = "path"),
    })
    public ResponseVO<Set<String>> getAuditPharmacist(@PathVariable("type") Integer type) {
        return PublicService.returnResponseVO(recommendService.getAuditPharmacist(type));
    }

}
